package com.imedway.ihe.dtz.xds.repository;

import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRetrieveDocumentSetResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType.DocumentRequest;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType;
import org.openehealth.ipf.commons.ihe.xds.core.requests.DocumentReference;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.imedway.ihe.dtz.xds.dao.Iti43Dao;
import com.imedway.ihe.dtz.xds.vo.RetrievedDocumentVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz<br/>
 * @date 2024/7/25
 */
@Slf4j
@Configuration
public class Iti43Repository {
	
	@Autowired
	private Iti43Dao iti43Dao;
	
	public RetrieveDocumentSetResponseType run(RetrieveDocumentSetRequestType body) {
		RetrieveDocumentSetResponseType retrieveDocumentSetResponseType = new RetrieveDocumentSetResponseType();
		retrieveDocumentSetResponseType.setRegistryResponse(new RegistryResponseType());
		EbXMLRetrieveDocumentSetResponse30 ebXMLRetrieveDocumentSetResponse30 = new EbXMLRetrieveDocumentSetResponse30(
				retrieveDocumentSetResponseType);
		List<EbXMLRegistryError> registryError = new ArrayList<EbXMLRegistryError>();
		boolean flag = true;
		try {
			List<DocumentRequest> documentRequestList = body.getDocumentRequest();
			List<RetrievedDocument> documentResponseList = new ArrayList<RetrievedDocument>();
			for (DocumentRequest documentRequest : documentRequestList) {
				if(documentRequest.getDocumentUniqueId()==null) {
					genError(registryError, Severity.ERROR, ErrorCode.STORED_QUERY_MISSING_PARAM.getOpcode(),"文档唯一id为空");
					ebXMLRetrieveDocumentSetResponse30.setErrors(registryError);
					return ebXMLRetrieveDocumentSetResponse30.getInternal();
				}
				documentResponseList.add(getRetrievedDocument(documentRequest));
			}
			ebXMLRetrieveDocumentSetResponse30.setDocuments(documentResponseList);			
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			genError(registryError, Severity.ERROR, ErrorCode.INVALID_REQUEST_EXCEPTION.getOpcode(),e.getMessage());
		}
		
		if(registryError!=null&&registryError.size()>0) {
			flag = false;
		}
		ebXMLRetrieveDocumentSetResponse30.setErrors(registryError);
		ebXMLRetrieveDocumentSetResponse30.setStatus(flag ? Status.SUCCESS : Status.FAILURE);
		
		return ebXMLRetrieveDocumentSetResponse30.getInternal();
	}
	
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}
	
	
	public RetrievedDocument getRetrievedDocument(DocumentRequest documentRequest) throws Exception {
//		Iti43Dao iti43Dao = new Iti43Dao();
		RetrievedDocumentVO retrievedDocumentVO = iti43Dao.findRetrievedDocument(documentRequest);					
		RetrievedDocument retrievedDocument = new RetrievedDocument();
		retrievedDocument.setMimeType(retrievedDocumentVO.getMimeType());
		retrievedDocument.setNewDocumentUniqueId(retrievedDocumentVO.getNewDocumentUniqueId());
		retrievedDocument.setNewRepositoryUniqueId(retrievedDocumentVO.getNewRepositoryUniqueId());
		retrievedDocument.setDataHandler(retrievedDocumentVO.getDataHandler());		
		retrievedDocument.setRequestData(new DocumentReference(documentRequest.getRepositoryUniqueId(),
				documentRequest.getDocumentUniqueId(),documentRequest.getHomeCommunityId()));		
		return retrievedDocument;

	}

}
